/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using Cysharp.Threading.Tasks;
using System;
using static IQIGame.Onigao.Game.MessageWrapper;

namespace NetProtocol.Client {
	/// <summary>
	/// 玩家加载模块
	/// module : loadPlayer
	/// </summary>
	public interface CNetLoadPlayer : INetClientModule {
		
		public ISession GetSession();
	
		Dictionary<int, INetClientModule.Package2MessageWrapper> INetClientModule.GetModuleFunction()
        {
            MessageFunction getRoleListResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<GetRoleListResultData>)wrapper;
                SC_getRoleListResult(session, ref _wrapper.message);
                MessageWrapperPool<GetRoleListResultData>.Put(_wrapper);
            };
            MessageFunction chooseRoleResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<ChooseRoleResultData>)wrapper;
                SC_chooseRoleResult(session, ref _wrapper.message);
                MessageWrapperPool<ChooseRoleResultData>.Put(_wrapper);
            };
            MessageFunction createRoleResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<CreateRoleResultData>)wrapper;
                SC_createRoleResult(session, ref _wrapper.message);
                MessageWrapperPool<CreateRoleResultData>.Put(_wrapper);
            };
            MessageFunction loadPlayerResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<LoadPlayerResultData>)wrapper;
                SC_loadPlayerResult(session, ref _wrapper.message);
                MessageWrapperPool<LoadPlayerResultData>.Put(_wrapper);
            };
        
            Dictionary<int, INetClientModule.Package2MessageWrapper> retDic = new()
            {
                {NetMessageId.loadPlayer_getRoleListResult, (session, package) => {
                	var result = MessageWrapperPool<GetRoleListResultData>.Get();
                	result.Init(session, getRoleListResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.loadPlayer_chooseRoleResult, (session, package) => {
                	var result = MessageWrapperPool<ChooseRoleResultData>.Get();
                	result.Init(session, chooseRoleResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.loadPlayer_createRoleResult, (session, package) => {
                	var result = MessageWrapperPool<CreateRoleResultData>.Get();
                	result.Init(session, createRoleResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.loadPlayer_loadPlayerResult, (session, package) => {
                	var result = MessageWrapperPool<LoadPlayerResultData>.Get();
                	result.Init(session, loadPlayerResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
            };
            return retDic;
        }
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
		public void SC_getRoleListResult(ISession session, ref GetRoleListResultData message) 
		{
            _SC_getRoleListResult(session, ref message);
		}
		
		public void _SC_getRoleListResult(ISession session, ref GetRoleListResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.loadPlayer_getRoleListResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.loadPlayer_getRoleListResult, message.code);
			MsgAsyncAwaiter<GetRoleListResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_chooseRoleResult(ISession session, ref ChooseRoleResultData message) 
		{
            _SC_chooseRoleResult(session, ref message);
		}
		
		public void _SC_chooseRoleResult(ISession session, ref ChooseRoleResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.loadPlayer_chooseRoleResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.loadPlayer_chooseRoleResult, message.code);
			MsgAsyncAwaiter<ChooseRoleResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_createRoleResult(ISession session, ref CreateRoleResultData message) 
		{
            _SC_createRoleResult(session, ref message);
		}
		
		public void _SC_createRoleResult(ISession session, ref CreateRoleResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.loadPlayer_createRoleResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.loadPlayer_createRoleResult, message.code);
			MsgAsyncAwaiter<CreateRoleResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_loadPlayerResult(ISession session, ref LoadPlayerResultData message) 
		{
            _SC_loadPlayerResult(session, ref message);
		}
		
		public void _SC_loadPlayerResult(ISession session, ref LoadPlayerResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.loadPlayer_loadPlayerResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.loadPlayer_loadPlayerResult, message.code);
			MsgAsyncAwaiter<LoadPlayerResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
#endregion
#region Server-side Interface
		/// <summary>
        /// 获取角色列表
        /// </summary>
    	public UniTask<GetRoleListResultData> CS_getRoleList()
        {
            return _CS_getRoleList();
        }
        
        public UniTask<GetRoleListResultData> _CS_getRoleList()
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(GetRoleListResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.loadPlayer_getRoleList, "Send->"+"LoadPlayer.CS_getRoleList { "
				+" }");
#endif
        	UniTask<GetRoleListResultData> task = MsgAsyncAwaiter<GetRoleListResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_getRoleList());
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.loadPlayer_getRoleListResult);
			return task;
        }
		
		public static SendPackage Pack_for_getRoleList() 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.loadPlayer_getRoleList;
			si_p.StartWrite();
			return si_p;
		}
		/// <summary>
        /// 选择角色进入游戏
        /// </summary>
    	public UniTask<ChooseRoleResultData> CS_chooseRole(long pid)
        {
            return _CS_chooseRole(pid);
        }
        
        public UniTask<ChooseRoleResultData> _CS_chooseRole(long pid)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(ChooseRoleResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.loadPlayer_chooseRole, "Send->"+"LoadPlayer.CS_chooseRole { "
				+ "pid="
				+ pid
				+" }");
#endif
        	UniTask<ChooseRoleResultData> task = MsgAsyncAwaiter<ChooseRoleResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_chooseRole(pid));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.loadPlayer_chooseRoleResult);
			return task;
        }
		
		public static SendPackage Pack_for_chooseRole(long pid) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.loadPlayer_chooseRole;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_long(pid);
			return si_p;
		}
		/// <summary>
        /// 创建角色
        /// </summary>
    	public UniTask<CreateRoleResultData> CS_createRole(int roleCid, string name)
        {
            return _CS_createRole(roleCid, name);
        }
        
        public UniTask<CreateRoleResultData> _CS_createRole(int roleCid, string name)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(CreateRoleResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.loadPlayer_createRole, "Send->"+"LoadPlayer.CS_createRole { "
				+ "roleCid="
				+ roleCid
				+ ", name="
				+ name
				+" }");
#endif
        	UniTask<CreateRoleResultData> task = MsgAsyncAwaiter<CreateRoleResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_createRole(roleCid, name));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.loadPlayer_createRoleResult);
			return task;
        }
		
		public static SendPackage Pack_for_createRole(int roleCid, string name) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.loadPlayer_createRole;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(roleCid);
			_out.Write_string(name);
			return si_p;
		}
		/// <summary>
        /// 加载玩家信息
        /// </summary>
    	public UniTask<LoadPlayerResultData> CS_loadPlayer()
        {
            return _CS_loadPlayer();
        }
        
        public UniTask<LoadPlayerResultData> _CS_loadPlayer()
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(LoadPlayerResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.loadPlayer_loadPlayer, "Send->"+"LoadPlayer.CS_loadPlayer { "
				+" }");
#endif
        	UniTask<LoadPlayerResultData> task = MsgAsyncAwaiter<LoadPlayerResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_loadPlayer());
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.loadPlayer_loadPlayerResult);
			return task;
        }
		
		public static SendPackage Pack_for_loadPlayer() 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.loadPlayer_loadPlayer;
			si_p.StartWrite();
			return si_p;
		}
#endregion
#region Messages
        public struct GetRoleListResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public List<RoleInfoPOD> roleList;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                roleList = _in.HasRemaining() ? _in.Read_list_pod<RoleInfoPOD>(RoleInfoPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.loadPlayer_getRoleListResult;}
            
            public override string ToString()
            {
                return "LoadPlayer.getRoleListResult"+ ", code="+ code+ ", roleList="+ roleList.ToString<RoleInfoPOD>();
            }
        }
        public struct ChooseRoleResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public string message;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                message = _in.HasRemaining() ? _in.Read_string() : default;
            }
            
            public int GetMessageId() {return NetMessageId.loadPlayer_chooseRoleResult;}
            
            public override string ToString()
            {
                return "LoadPlayer.chooseRoleResult"+ ", code="+ code+ ", message="+ message;
            }
        }
        public struct CreateRoleResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public RoleInfoPOD role;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                role = _in.HasRemaining() ? _in.Read_pod<RoleInfoPOD>(RoleInfoPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.loadPlayer_createRoleResult;}
            
            public override string ToString()
            {
                return "LoadPlayer.createRoleResult"+ ", code="+ code+ ", role="+ role;
            }
        }
        public struct LoadPlayerResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public PlayerPOD player;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                player = _in.HasRemaining() ? _in.Read_pod<PlayerPOD>(PlayerPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.loadPlayer_loadPlayerResult;}
            
            public override string ToString()
            {
                return "LoadPlayer.loadPlayerResult"+ ", code="+ code+ ", player="+ player;
            }
        }
    }
#endregion
}